<template>
  <el-dialog
    v-model="visible"
    title="设置邮编分区"
    append-to-body
    :close-on-click-modal="false"
  >
    <el-row :gutter="10" class="mb8">
      <el-col v-hasPermi="['system:costPricPostcode:add']" :span="1.5">
        <el-button type="primary" @click="handleAdd">增加</el-button>
      </el-col>
    </el-row>
    <el-form ref="formRef" :model="form">
      <el-table :data="form.tableData" style="width: 100%;">
        <el-table-column type="selection"></el-table-column>
        <el-table-column label="开始邮编">
          <template #default="{row,$index}">
            <el-form-item :prop="`tableData.${$index}.postcodeStart`" :rules="[{ required: true, message: '必填', trigger: 'blur' }]">
              <el-input v-model.trim="row.postcodeStart"></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column label="结束邮编" style="margin-right: 20px;">
          <template #default="{row,$index}">
            <el-form-item :prop="`tableData.${$index}.postcodeEnd`" :rules="[{ required: true, message: '必填', trigger: 'blur' }]">
              <el-input v-model="row.postcodeEnd"></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right">
          <template #default="{row,$index}">
            <el-button
              v-hasPermi="['system:costPricPostcode:remove']"
              text
              type="primary"
              :icon="Delete" 
              @click="handleDelete(row, $index)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <template #footer>
      <div>
        <el-button type="primary" :loading="loading" @click="submitForm">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { Delete } from '@element-plus/icons-vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import { validateForm } from '@/utils'
import {
  addCodeList,
  delCodeList,
  getCodeList,
} from '@/api/BusinessConfig/costCountryPartition'

const visible = ref<boolean>(false)
const loading = ref<boolean>(false)
const countryPartitionId = ref<number>()
const formRef = ref()
const form = ref({
  tableData: [],
})
const getList = () => {
  getCodeList({
    countryPartitionId: countryPartitionId.value,
  }).then((res) => {
    if (res.code === 200) {
      form.value.tableData = res.data
    }
  })
}
// 新增邮编分区
const handleAdd = () => {
  form.value.tableData.push({
    postcodeStart: '',
    postcodeEnd: '',
  })
}
const handleDelete = (index, row) => {
  if (row.id != undefined) {
    ElMessageBox.confirm('确定删除嘛？', '警告', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(() => {
      delCodeList(row.id).then(res => {
        res.code == 200 && getList()
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })
  } else {
    form.value.tableData.splice(index, 1)
  }
}
const open = (id:number) => {
  countryPartitionId.value = id
  visible.value = true
  getList()
}
const submitForm = () => {
  validateForm(formRef.value, () => {
    const data = {
      countryPartitionId: countryPartitionId.value,
      partitionPostcodeList: form.value.tableData,
    }
    loading.value = true
    addCodeList(data).then((res) => {
      res.code === 200 && ElMessage.success('保存成功') && getList()
    })
    .finally(() => {
      loading.value = false
    })
  })
}
const cancel = () => {
  formRef.value.resetFields()
  visible.value = false
}
defineExpose({
  open,
})
</script>